package com.singlemusic.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.singlemusic.pojo.Users;
import com.singlemusic.service.impl.UserService;
import com.singlemusic.utils.MD5Utils;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource(name="userService")
	private UserService userService;
	@RequestMapping(value="/userReg",method=RequestMethod.POST)
	public String regUser(@ModelAttribute Users users){
		String pwd = users.getUserpassword();
		users.setRegdate(new Date().toString());
		users.setUserpassword(MD5Utils.GetMD5Code(pwd));
		boolean flag = userService.userReg(users);
		if(flag==true){
			System.out.println("注册成功");
		}else{
			System.out.println("注册失败");
		}
		return null;
	}
	@RequestMapping(value="/userLogin",method=RequestMethod.POST)
	public String loginUser(@RequestParam("userid")String userid,@RequestParam("userpwd")String userpwd,HttpSession session){
		Users users = userService.login(userid);
		
		String msg = "登录成功";
		if(users==null){
			msg = "用户名不存在";
			session.setAttribute("msg", msg);
			return "redirect:/login.jsp";
		}else{
			String pwd = users.getUserpassword();
			if(!pwd.equals(MD5Utils.GetMD5Code(userpwd))){
				msg = "密码错误";
				session.setAttribute("msg", msg);
				return "redirect:/login.jsp";
			}else{
				session.setAttribute("users", users);
				return "redirect:/index.jsp";
			}
		}
	}
	
	@RequestMapping("/count")
	public void count(){
		String sql = "select * from users where username=?";
		String[] param = {"丁成文"};
		List<Users> list = userService.findBySql(sql, param);
		for(Users u:list){
			System.out.println(u.getUserid());
		}
	}
}
